package cn.edu.scau.cmi.ema.controller.rest;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.io.IOException;

import com.google.gson.Gson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiPsonService;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;

import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.WebDataBinder;

@Controller("CrawlerfilestandardcolumnRestController")
public class CrawlerfilestandardcolumnRestController extends CmiEmaController {

	/**
	 * Create a new Crawlerfilestandardcolumn entity
	 * 
	 */
	@RequestMapping(value = "/newCrawlerfilestandardcolumn", method = RequestMethod.POST)
	@ResponseBody
	public Object newCrawlerfilestandardcolumn(@RequestBody Crawlerfilestandardcolumn crawlerfilestandardcolumn) {
		crawlerfilestandardcolumnService.saveCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
		return JSONObject.parse(cmiPsonService.toJsonString(crawlerfilestandardcolumnDAO
				.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumn.getId()), 2));
	}

	@RequestMapping(value = "/restNewCrawlerfilestandardcolumn", method = RequestMethod.POST)
	@ResponseBody
	public Object restNewCrawlerfilestandardcolumn(@RequestBody String crawlerfilestandardcolumnJson) {
		Crawlerfilestandardcolumn crawlerfilestandardcolumn = new Gson().fromJson(crawlerfilestandardcolumnJson,
				Crawlerfilestandardcolumn.class);
		appendReferences(crawlerfilestandardcolumn);
		crawlerfilestandardcolumnService.saveCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
		return JSONObject.parse(cmiPsonService.toJsonString(crawlerfilestandardcolumnDAO
				.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumn.getId()), 2));
	}

	@RequestMapping(value = "/saveCrawlerfilestandardcolumn", method = RequestMethod.PUT)
	@ResponseBody
	public Crawlerfilestandardcolumn saveCrawlerfilestandardcolumn(
			@RequestBody Crawlerfilestandardcolumn crawlerfilestandardcolumn) {
		crawlerfilestandardcolumnService.saveCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
		return crawlerfilestandardcolumnDAO
				.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumn.getId());
	}

	@RequestMapping(value = "/restSaveCrawlerfilestandardcolumn", method = RequestMethod.PUT)
	@ResponseBody
	public Object restSaveCrawlerfilestandardcolumn(@RequestBody String crawlerfilestandardcolumnJson) {
		Crawlerfilestandardcolumn crawlerfilestandardcolumn = new Gson().fromJson(crawlerfilestandardcolumnJson,
				Crawlerfilestandardcolumn.class);
		appendReferences(crawlerfilestandardcolumn);
		CmiPsonService cmiPsonService = new CmiPsonService();
		crawlerfilestandardcolumnService.saveCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
		return JSONObject.parse(cmiPsonService.toJsonString(crawlerfilestandardcolumnDAO
				.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumn.getId()), 1));
	}

	@RequestMapping(value = "/restDeleteCrawlerfilestandardcolumn/{crawlerfilestandardcolumnId}", method = RequestMethod.DELETE)
	@ResponseBody
	public void restDeleteCrawlerfilestandardcolumn(@PathVariable Integer crawlerfilestandardcolumnId) {
		Crawlerfilestandardcolumn crawlerfilestandardcolumn = crawlerfilestandardcolumnDAO
				.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumnId);
		crawlerfilestandardcolumnService.deleteCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
	}

	@RequestMapping(value = "/restConfirmDeleteCrawlerfilestandardcolumns")
	@ResponseBody
	public Object restConfirmDeleteCrawlerfilestandardcolumns(
			@RequestBody Set<Integer> deleteCrawlerfilestandardcolumnsIds) {
		Set<Crawlerfilestandardcolumn> deleteCrawlerfilestandardcolumns = crawlerfilestandardcolumnDAO
				.findCrawlerfilestandardcolumnsByIds(deleteCrawlerfilestandardcolumnsIds);
		Set<Crawlerfilestandardcolumn> referencedCrawlerfilestandardcolumns = crawlerfilestandardcolumnService
				.loadReferenceCrawlerfilestandardcolumns(deleteCrawlerfilestandardcolumns);
		deleteCrawlerfilestandardcolumns.removeAll(referencedCrawlerfilestandardcolumns);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("referencedEntitys", referencedCrawlerfilestandardcolumns);
		jsonObject.put("deleteEntitys", deleteCrawlerfilestandardcolumns);
		return JSONObject.parse(jsonObject.toJSONString());
	}

	@RequestMapping("/restDeleteCrawlerfilestandardcolumns")
	@ResponseBody
	public Object restDeleteCrawlerfilestandardcolumns(
			@RequestBody Set<Crawlerfilestandardcolumn> deleteCrawlerfilestandardcolumns) {
		Iterator<Crawlerfilestandardcolumn> iterator = deleteCrawlerfilestandardcolumns.iterator();
		while (iterator.hasNext()) {
			Crawlerfilestandardcolumn currentCrawlerfilestandardcolumn = iterator.next();
			crawlerfilestandardcolumnService.deleteCrawlerfilestandardcolumn(currentCrawlerfilestandardcolumn);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("url", "listCrawlerfilestandardcolumns");
		return JSONObject.parse(jsonObject.toJSONString());
	}
	// loadself

	@RequestMapping(value = "/restLoadCrawlerfilestandardcolumn/{crawlerfilestandardcolumnId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadCrawlerfilestandardcolumn(@PathVariable Integer crawlerfilestandardcolumnId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				crawlerfilestandardcolumnDAO.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumnId), 2,
				cmiPagination));
	}
	// loadReferenceds
	// TODO loadReferencedsPson，这个方法有一些问题，待修复

	@RequestMapping(value = "/restLoadCrawlerfilestandardcolumnCrawlerfilecolumns/{crawlerfilestandardcolumnId}/crawlerfilecolumns/{relatedcrawlerfilecolumnId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadCrawlerfilestandardcolumnCrawlerfilecolumns(@PathVariable Integer crawlerfilestandardcolumnId,
			@PathVariable Integer relatedcrawlerfilecolumnId, @RequestBody CmiPagination cmiPagination) {
		Crawlerfilecolumn crawlerfilecolumn = crawlerfilecolumnDAO
				.findCrawlerfilecolumnByPrimaryKey(relatedcrawlerfilecolumnId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(crawlerfilecolumn, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadCrawlerfilestandardcolumnCrawlerfilestandardetails/{crawlerfilestandardcolumnId}/crawlerfilestandardetails/{relatedcrawlerfilestandardetailId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadCrawlerfilestandardcolumnCrawlerfilestandardetails(
			@PathVariable Integer crawlerfilestandardcolumnId, @PathVariable Integer relatedcrawlerfilestandardetailId,
			@RequestBody CmiPagination cmiPagination) {
		Crawlerfilestandardetail crawlerfilestandardetail = crawlerfilestandardetailDAO
				.findCrawlerfilestandardetailByPrimaryKey(relatedcrawlerfilestandardetailId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(crawlerfilestandardetail, 2, cmiPagination));
	}

	public void appendReferences(Crawlerfilestandardcolumn crawlerfilestandardcolumn) {

		// 引用表是纯联系表的情况
		Set<Crawlerfilecolumn> crawlerfilecolumnsForCrawlerfilestandardcolumn = new HashSet<>();
		for (Crawlerfilecolumn crawlerfilecolumnForCrawlerfilestandardcolumn : crawlerfilestandardcolumn
				.getCrawlerfilecolumnsForCrawlerfilestandardcolumn()) {
			crawlerfilecolumnsForCrawlerfilestandardcolumn.add(crawlerfilecolumnService
					.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumnForCrawlerfilestandardcolumn.getId()));
		}
		crawlerfilestandardcolumn
				.setCrawlerfilecolumnsForCrawlerfilestandardcolumn(crawlerfilecolumnsForCrawlerfilestandardcolumn);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Crawlerfilestandardetail> crawlerfilestandardetailsForCrawlerfilestandardcolumn = new HashSet<>();
		for (Crawlerfilestandardetail crawlerfilestandardetail_Crawlerfilestandardcolumn : crawlerfilestandardcolumn
				.getCrawlerfilestandardetailsForCrawlerfilestandardcolumn()) {
			crawlerfilestandardetailsForCrawlerfilestandardcolumn
					.add(crawlerfilestandardetailService.findCrawlerfilestandardetailByPrimaryKey(
							crawlerfilestandardetail_Crawlerfilestandardcolumn.getId()));
		}
		crawlerfilestandardcolumn.setCrawlerfilestandardetailsForCrawlerfilestandardcolumn(
				crawlerfilestandardetailsForCrawlerfilestandardcolumn);
	}

	@RequestMapping(value = "/restGetCrawlerfilestandardcolumnCrawlerfilecolumns/{crawlerfilestandardcolumnId}/crawlerfilecolumns", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetCrawlerfilestandardcolumnCrawlerfilecolumns(@PathVariable Integer crawlerfilestandardcolumnId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				crawlerfilestandardcolumnDAO.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumnId)
						.getCrawlerfilecolumnsForCrawlerfilestandardcolumn(),
				2, cmiPagination));
	}

	@RequestMapping(value = "/restGetCrawlerfilestandardcolumnCrawlerfilestandardetails/{crawlerfilestandardcolumnId}/crawlerfilestandardetails", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetCrawlerfilestandardcolumnCrawlerfilestandardetails(
			@PathVariable Integer crawlerfilestandardcolumnId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				crawlerfilestandardcolumnDAO.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumnId)
						.getCrawlerfilestandardetailsForCrawlerfilestandardcolumn(),
				2, cmiPagination));
	}

	@RequestMapping(value = "/restListCrawlerfilestandardcolumns", method = RequestMethod.GET)
	@ResponseBody
	public Object restListCrawlerfilestandardcolumns() {
		return JSONObject
				.parse(cmiPsonService.toJsonString(crawlerfilestandardcolumnService.loadCrawlerfilestandardcolumns(), 2));
	}

	@RequestMapping(value = "/restListCrawlerfilestandardcolumnsPost", method = RequestMethod.POST)
	@ResponseBody
	public Object restListCrawlerfilestandardcolumnsPost(@RequestBody CmiPagination cmiPagination) {

		CmiPsonService cmiPsonService = new CmiPsonService();
		cmiPagination = crawlerfilestandardcolumnPaginationService.updateAppPagination(cmiPagination);
		int startResult = (cmiPagination.getCurrentPagination() - 1) * cmiPagination.getPaginationSize();

		Set<Crawlerfilestandardcolumn> crawlerfilestandardcolumns = crawlerfilestandardcolumnDAO
				.findAllCrawlerfilestandardcolumns(startResult, cmiPagination.getPaginationSize());
		String crawlerfilestandardcolumnString = cmiPsonService.toPsonString(crawlerfilestandardcolumns, 2, cmiPagination);

		return JSONObject.parse(crawlerfilestandardcolumnString);
	}

	@RequestMapping("/crawlerfilestandardcolumnSelectBox")
	@ResponseBody
	public JSONObject crawlerfilestandardcolumnSelectBox(@RequestParam(required = false) String searchTable,
			@RequestParam(required = false) Set<Integer> searchValue, // 杨宏宇2019-9-10：searchValue是在selectpage.js的616行定义，如果要修改searchValue的名称，1.可以修改在selectpage.js修改；2.可以
																		// @RequestParam(value="searchValue", required =
																		// false) Set<Integer> someName
			@RequestParam(required = false, defaultValue = "10") int paginationSize,
			@RequestParam(required = false, defaultValue = "1") int pageNumber,
			@RequestParam(value = "qWord[]", required = false) String queryword,
			@RequestParam(required = false) String selectItem) throws IOException {
		Set entitySet = null;
		Integer count = 0;

		// 初始化原始值

		if (searchValue != null) {
			switch (searchTable) {
			// 外键

			// 被引用键

			case "Crawlerfilecolumn":
				entitySet = crawlerfilecolumnService.loadCrawlerfilecolumnsByIdSet(searchValue);
				break;
			case "Crawlerfilestandardetail":
				entitySet = crawlerfilestandardetailService.loadCrawlerfilestandardetailsByIdSet(searchValue);
				break;
			}
			// 没有测试，深度需要多少？？？
			String jsonData = cmiPsonService.toJsonString(entitySet, 1);
			JSONArray jsonArray = new JSONArray();
			jsonArray = (JSONArray) JSON.parse((String) jsonData);

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("list", jsonArray);
			jsonObject.put("totalRow", entitySet.size());
			return jsonObject;
		}

		// 不根据关键字进行搜索
		if (queryword == null || queryword.trim() == "") {
			switch (selectItem) {
			// 外键

			// 被引用键

			case "Crawlerfilecolumn":
				entitySet = crawlerfilecolumnDAO.findAllCrawlerfilecolumns((pageNumber - 1) * paginationSize,
						paginationSize);
				count = crawlerfilecolumnService.countCrawlerfilecolumns();
				break;
			case "Crawlerfilestandardetail":
				entitySet = crawlerfilestandardetailDAO
						.findAllCrawlerfilestandardetails((pageNumber - 1) * paginationSize, paginationSize);
				count = crawlerfilestandardetailService.countCrawlerfilestandardetails();
				break;
			}
		} else {
			switch (selectItem) {
			// 外键

			// 被引用键

			case "Crawlerfilecolumn":
				entitySet = crawlerfilecolumnDAO.findCrawlerfilecolumnByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = crawlerfilecolumnService.countCrawlerfilecolumnsByName(queryword);
				break;
			case "Crawlerfilestandardetail":
				entitySet = crawlerfilestandardetailDAO.findCrawlerfilestandardetailByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = crawlerfilestandardetailService.countCrawlerfilestandardetailsByName(queryword);
				break;
			}
		}
		/**
		 * 将Json数据转换成jsonArray形式，需要通过pson方式进行转换，然后再转换成JsonArray。因为存在懒加载
		 * 没有直接使用CmiPsonService中的fromJson方法是因为，fromJson方法会将int类型的直接转换成double类型的数据，这个问题还没有解决
		 */
		// 没有测试，深度需要多少？？？
		String jsonData = cmiPsonService.toJsonString(entitySet, 1);
		JSONArray jsonArray = new JSONArray();
		jsonArray = (JSONArray) JSON.parse((String) jsonData);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("list", jsonArray);
		jsonObject.put("paginationSize", paginationSize);
		jsonObject.put("pageNumber", pageNumber);
		jsonObject.put("totalRow", count);
		jsonObject.put("totalPaginations",
				count % paginationSize == 0 ? count / paginationSize : count / paginationSize + 1);
		return jsonObject;
	}
}
